package com.springmvc.controller;

import com.springmvc.model.IncomeTable;
import com.springmvc.service.IncomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value="/income")
public class IncomeController {
    @Autowired
    IncomeService incomeService;
    @RequestMapping(value="incomeList")
    public ModelAndView incomeList(){
        ModelAndView mv = new ModelAndView();
        List<IncomeTable> incomeTablesList = incomeService.queryByPage();
        Double totalMoney = 0.00;
        for (int i = 0; i < incomeTablesList.size(); i++) {
            totalMoney += incomeTablesList.get(i).getIncome();
        }
        mv.addObject("incomeTablesList",incomeTablesList);
        mv.addObject("totalMoney",totalMoney);
        mv.setViewName("test/incomeList");
        return mv;
    }

    @RequestMapping(value="addForm")
    public ModelAndView addForm(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("test/addForm");
        return mv;
    }

    @RequestMapping(value="addIncome")
    @ResponseBody
    public Object addIncome(String content, Float income,String remark){
        IncomeTable incomeTable1 = new IncomeTable();
        incomeTable1.setContent(content);
        incomeTable1.setIncome(income);
        incomeTable1.setRemark(remark);
        incomeTable1.setCreatetime(new Date());
        incomeService.insert(incomeTable1);
        return "ok";
    }
}